var app = new Vue({
    el: '.container',
    computed: {
        exchangenum: {//兑换数量
            get() {
                if (!isNaN(Number(this.exchange.exchangenum))) {
                    return this.exchange.exchangenum
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0) {
                        this.exchange.exchangenum = 0
                    } else {
                        this.exchange.exchangenum = parseFloat(v)
                    }
                    if (parseFloat(Number(v)) > parseFloat(this.userinfo.banlance)) {
                        this.exchange.exchangenum = parseInt(this.userinfo.banlance)
                    }
                    this.exchange.exchangenum = Math.floor(this.exchange.exchangenum * 100) / 100
                    let computedresult = parseFloat(Number(this.exchange.exchangenum)) * this.exchange.agratenum
                    if (!isNaN(Number(computedresult))) {
                        this.exchange.exchangeagcoin = computedresult
                        if (this.exchange.exchangeagcoin > 3000) {
                            this.exchange.disexchangenum = true
                            let lang = this.getCookie("lang");
                            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                            var b = '<div class="maskLayer"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                                this.exchange.disexchangenum = false
                                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                                document.querySelector('.container').removeChild(c)
                                document.querySelector('.container').removeChild(d)
                            }
                            if (lang == "en_US") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "The amount is greater than 3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "The maximum redemption limit for saba points is 3000"
                            } else if (lang == "zh_TW") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "額度大於3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "saba分最大兌換額度為3000"
                            } else {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "额度大于3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "saba分最大兑换额度为3000"
                            }
                        }
                    } else {
                        this.exchange.exchangeagcoin = 0
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        },
        cashoutnum: { //兑出数量
            get() {
                if (!isNaN(Number(this.cashout.exchangenum))) {
                    if (parseFloat(this.cashout.exchangenum) >= 0) {
                        return this.cashout.exchangenum
                    } else {
                        return 0
                    }
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0) {
                        this.cashout.exchangenum = 0
                    } else {
                        this.cashout.exchangenum = parseInt(v)
                    }
                    if (parseFloat(Number(v)) > parseFloat(this.userinfo.myagbanlance)) {
                        this.cashout.exchangenum = parseInt(this.userinfo.myagbanlance)
                    }
                    this.cashout.exchangenum = Math.floor(this.cashout.exchangenum * 100) / 100
                    let computedresult = parseFloat(Number(this.cashout.exchangenum)) / parseFloat(this.exchange.agratenum)
                    computedresult = parseFloat(computedresult.toFixed(8))
                    if (!isNaN(Number(computedresult))) {
                        this.cashout.exchangeagcoin = computedresult
                    } else {
                        this.cashout.exchangeagcoin = 0
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        }
    },
    mounted() {
        if (localStorage.getItem('ID') != null || localStorage.getItem('ID') != '') {
            this.getagBalance()
            this.getagDict(this.exchange.defaultcoin)
            this.getsabalogin(this.exchange.defaultcoin)
        }
    },
    data() {
        return {
            showtransforminag: false,//兑换AG分
            showtransformout: false,//兑出AG分
            showText: false,
            hideText: true,
            exchange: { //沙巴兑换配置项
                defaultcoin:window.sessionStorage.getItem('sabacoin')?window.sessionStorage.getItem('sabacoin'):'HPS', //默认币种
                isshowcoinarr: false, //是否展示下拉选
                exchangenum: '0', //兑换数量
                disexchangenum: false,//禁用兑换数量
                exchangeagcoin: '0',//兑换后数量
                agratenum: 5, //AG兑换比例
                disabledbtn: false //禁用兑换点击按钮
            },
            cashout: { //沙巴兑出配置项
                exchangenum: '0', //兑换数量
                exchangeagcoin: '0',//兑换后币种数量
                disabledbtn: false //禁用兑出点击按钮
            },
            userinfo: { //用户信息
                banlance: '',//用户余额
                myagbanlance: 0,//我的沙巴积分余额
                sabaloginaddress:'' //用户沙巴登录地址
            },
            theuser: "",
            information: "",
            loading: false
        }
    },
    methods: {
        getagBalance() {//获取当前用户沙巴积分
            let params = {
                coin: this.exchange.defaultcoin
            }
            axios.post('/sabauserBalance',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data){
                        if(res.data.data.Data&&res.data.data.Data.length){
                            this.userinfo.myagbanlance = res.data.data.Data[0].balance
                        }
                    }else{
                        this.userinfo.myagbanlance = 0
                    }
                }
            })
        },
        changetransform(keyname) { //用户点击兑换、兑出沙巴分栏目
            if (!keyname) {
                return
            }
            this.$data[keyname] = !this.$data[keyname]
            if (keyname == 'showtransforminag') { //兑换AG分
                this.showtransformout = false
                if (this.$data[keyname]) {
                    this.getagDict(this.exchange.defaultcoin) //根据币种换算兑换比例
                }
            } else if (keyname == 'showtransformout') {//兑出AG分
                this.showtransforminag = false
            }
        },
        changeexchangecoin(cointype, banlance) {//用户点击更换币种
            this.exchange.defaultcoin = cointype
            this.exchange.isshowcoinarr = false
            this.userinfo.banlance = banlance
            window.sessionStorage.setItem('sabacoin',cointype)
            this.getagDict(cointype)
            this.getagBalance()
            this.getsabalogin(cointype)
        },
        getagDict(coinname) { //选择币种获取沙巴兑换比例
            if (!coinname) {
                return
            }
            axios.get(`/ag/dict/${coinname}`, {}).then((res) => {
                if(res.data.data&&res.data.data.dictValue){
                    this.exchange.agratenum = parseFloat(res.data.data.dictValue)
                }else{
                    this.exchange.agratenum = 5
                }
            })
        },
        confirmexchange() { //确定兑换沙巴分
            //为空、NaN、
            if (
                this.exchange.exchangenum == '' || isNaN(Number(this.exchange.exchangenum)) || this.exchange.exchangenum == 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌換提示',
                        title2:'請輸入兌換數量大於零'
                    },
                    zh_CN:{
                        title1:'兑换提示',
                        title2:'请输入兑换数量大于零'
                    },
                    en_US:{
                        title1:'Exchange tips',
                        title2:'Redemption quantity is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            if (parseFloat(Number(this.exchange.exchangenum)) > parseFloat(this.userinfo.banlance)) { //兑换数量>用户余额
                let lang = this.getCookie("lang");
                var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                var b = '<div class="maskLayer"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                document.getElementsByClassName("closeinfo")[0].onclick = () => {
                    var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                    var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                    document.querySelector('.container').removeChild(c)
                    document.querySelector('.container').removeChild(d)
                }
                if (lang == "en_US") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "Lack of balance"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "Insufficient account balance"
                } else if (lang == "zh_TW") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "餘額不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "賬戶餘額不足！"
                } else {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "余额不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "账户余额不足！"
                }
                return
            }
            this.exchange.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.exchange.exchangenum),
                direction:1
            }
            axios.post('/sabafundTransfer',params).then( (res) => {
                this.exchange.disabledbtn = false
                if(res.data.code==200){
                    if(res.data.data&&res.data.data.Data){
                        let dialogopt={
                            zh_TW:{
                                title1:'兌換成功',
                                title2:'saba積分兌換成功！'
                            },
                            zh_CN:{
                                title1:'兑换成功',
                                title2:'saba积分兑换成功！'
                            },
                            en_US:{
                                title1:'Successfully redeemed',
                                title2:'Successfully converted the saba points.'
                            }
                        }
                        this.opendialog(dialogopt)
                        this.userinfo.myagbanlance=res.data.data.Data.after_amount
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'請稍後重試！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later!'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                    this.getsabalogin(this.exchange.defaultcoin)
                }else{
                    let dialogopt={
                        zh_TW:{
                            title1:'提示',
                            title2:'請稍後重試！'
                        },
                        zh_CN:{
                            title1:'提示',
                            title2:'请稍后重试！'
                        },
                        en_US:{
                            title1:'Prompt',
                            title2:'Please try again later!'
                        }
                    }
                    this.opendialog(dialogopt)
                }
            })
        },
        confircashout() { //从沙巴兑回
            if (
                localStorage.getItem('ID') == '' ||
                localStorage.getItem('ID') == ''
            ) {
                location.href = '/singin'
                return
            }
            if (
                this.userinfo.myagbanlance == '' ||
                this.cashout.exchangenum == 0 ||
                this.cashout.exchangenum == '' ||
                isNaN(Number(this.userinfo.myagbanlance)) ||
                parseFloat(this.userinfo.myagbanlance) <= 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌回提示',
                        title2:'請輸入兌回數量大於零'
                    },
                    zh_CN:{
                        title1:'兑回提示',
                        title2:'请输入兑回数量大于零'
                    },
                    en_US:{
                        title1:'Redemption tips',
                        title2:'The amount of redemption is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            this.cashout.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount:parseFloat(this.cashout.exchangenum),
                direction:0
            }
            axios.post('/sabafundTransfer',params).then( (res) => {
                this.cashout.disabledbtn = false
                if(res.data.code==200){
                    if(res.data.data.Data){
                        let dialogopt={
                            zh_TW:{
                                title1:'兌出成功',
                                title2:'saba積分兌出成功！'
                            },
                            zh_CN:{
                                title1:'兑出成功',
                                title2:'saba积分兑出成功！'
                            },
                            en_US:{
                                title1:'Successfully redeemed',
                                title2:'Saba points out successful.'
                            }
                        }
                        this.opendialog(dialogopt)
                        this.userinfo.myagbanlance=res.data.data.Data.after_amount
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'請稍後重試！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later!'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                    this.getsabalogin(this.exchange.defaultcoin)
                }else{
                    let dialogopt={
                        zh_TW:{
                            title1:'提示',
                            title2:'請稍後重試！'
                        },
                        zh_CN:{
                            title1:'提示',
                            title2:'请稍后重试！'
                        },
                        en_US:{
                            title1:'Prompt',
                            title2:'Please try again later!'
                        }
                    }
                    this.opendialog(dialogopt)
                }
            })
        },
        opendialog(obj) { //打开遮罩层
            let lang = this.getCookie("lang");
            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
            var b = '<div class="maskLayer"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                document.querySelector('.container').removeChild(c)
                document.querySelector('.container').removeChild(d)
            }
            document.getElementsByClassName('errorMessageTop')[0].innerHTML = obj[lang].title1
            document.getElementsByClassName('errorMessageBottom')[0].innerHTML = obj[lang].title2
        },
        closeinfo() { //关闭广告窗
            document.getElementsByClassName('maskLayer')[0].style.display = "none"
            document.getElementsByClassName('errorMessage')[0].style.display = "none"
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        getCookie(cookiename) { //获取cookie
            var arr, reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if (arr = document.cookie.match(reg)) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        getsabalogin(cointype){ //请求沙巴登录
            let params = {
                coin: cointype
            }
            this.loading = false
            axios.post('/sabalogin',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data&&res.data.data.error_code==0){
                        // https://mkt.gsoft-ib.com
                        this.userinfo.sabaloginaddress=`https://mkt.8822882.com/Deposit_ProcessLogin.aspx?lang=cs&webskintype=3&g=${res.data.data.sessionToken}`
                    }else{
                        this.userinfo.sabaloginaddress=`https://mkt.8822882.com/vender.aspx?lang=cs`
                    }
                    this.loading = true
                }else{
                    this.userinfo.sabaloginaddress=`https://mkt.8822882.com/vender.aspx?lang=cs`
                    this.loading = true
                }
            })
        }
    }
})